/*DONE* Table [dbo].[<CompanyName>$LSC Posted Customer Order Line$5ecfc871-5d82-43f1-9c54-59685e82318d]  1/1  Script Date: 14.1.2021 - JI ******/
/*Add prestaging tables for all companies in LSInsight$Companies table*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyCounter as INT
DECLARE @i as INT

SET @i = 0
SET @CompanyCounter = (SELECT
		MAX([Company])
	FROM LSInsight$Companies)

WHILE @i <= @CompanyCounter 
BEGIN
SET @Company = (SELECT
		LSInsight$Companies.[CompanyPrefix]
	FROM dbo.LSInsight$Companies
	WHERE [Company] = @i)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].[' + @Company + '$LSC Posted Customer Order Line$5ecfc871-5d82-43f1-9c54-59685e82318d]'
EXEC (@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].[' + @Company + '$LSC Posted Customer Order Line$5ecfc871-5d82-43f1-9c54-59685e82318d](
		[timestamp] [timestamp] NOT NULL,
	[Document ID] [nvarchar](20) NOT NULL,
	[Line No_] [int] NOT NULL,
	[Status] [int] NOT NULL,
	[Line Type] [int] NOT NULL,
	[Number] [nvarchar](20) NOT NULL,
	[Variant Code] [nvarchar](10) NOT NULL,
	[Unit of Measure Code] [nvarchar](10) NOT NULL,
	[Net Price] [decimal](38, 20) NOT NULL,
	[Price] [decimal](38, 20) NOT NULL,
	[Quantity] [decimal](38, 20) NOT NULL,
	[Discount Amount] [decimal](38, 20) NOT NULL,
	[Discount Percent] [decimal](38, 20) NOT NULL,
	[Net Amount] [decimal](38, 20) NOT NULL,
	[Vat Amount] [decimal](38, 20) NOT NULL,
	[Amount] [decimal](38, 20) NOT NULL,
	[Vat Prod_ Posting Group] [nvarchar](20) NOT NULL,
	[Item Description] [nvarchar](100) NOT NULL,
	[Variant Description] [nvarchar](100) NOT NULL,
	[UoM Description] [nvarchar](50) NOT NULL,
	[Ship Order] [tinyint] NOT NULL,
	[Original Line No_] [int] NOT NULL,
	[Internal Selection] [tinyint] NOT NULL,
	[Internal Available Qty_] [decimal](38, 20) NOT NULL,
	[Sourcing Location] [nvarchar](10) NOT NULL,
	[Order Reference] [nvarchar](50) NOT NULL,
	[Inventory Transfer] [tinyint] NOT NULL,
	[Vendor Sourcing] [tinyint] NOT NULL,
	[Lead Time] [varchar](32) NOT NULL,
	[Prepayment Amount] [decimal](38, 20) NOT NULL,
	[Purchase Order No_] [nvarchar](20) NOT NULL,
	[Quantity Received] [decimal](38, 20) NOT NULL,
	[External ID] [nvarchar](50) NOT NULL,
	[Retail Image ID] [nvarchar](20) NOT NULL,
	[Quantity (Base)] [decimal](38, 20) NOT NULL,
	[Requested Delivery Date] [datetime] NOT NULL,
	[Store No_] [nvarchar](10) NOT NULL,
	[Collect Shelf] [nvarchar](20) NOT NULL,
	[Terminal No_] [nvarchar](10) NOT NULL,
	[Collect Time Limit] [datetime] NOT NULL,
	[Click and Collect Line] [tinyint] NOT NULL,
	[Shipping Agent Code] [nvarchar](10) NOT NULL,
	[Shipping Agent Service Code] [nvarchar](10) NOT NULL,
	[Sourcing Order Type] [int] NOT NULL,
	[Put Back Item (Internal)] [tinyint] NOT NULL,
	[Payment Registered (Internal)] [tinyint] NOT NULL,
	[$systemId] [uniqueidentifier] NOT NULL,
	[$systemCreatedAt] [datetime] NOT NULL,
	[$systemCreatedBy] [uniqueidentifier] NOT NULL,
	[$systemModifiedAt] [datetime] NOT NULL,
	[$systemModifiedBy] [uniqueidentifier] NOT NULL,
	[Box No_] [nvarchar](20) NOT NULL,
 CONSTRAINT [' + @Company + '$LSC Posted Customer Order Line$5ecfc871-5d82-43f1-9c54-59685e82318d$Key1] PRIMARY KEY CLUSTERED 
(
	[Document ID] ASC,
	[Line No_] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT [' + @Company + '$LSC Posted Customer Order Line$5ecfc871-5d82-43f1-9c54-59685e82318d$$systemId] UNIQUE NONCLUSTERED 
(
	[$systemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]'

EXEC(@CreateTable)

END
SET @i = @i + 1
END
